package com.ibm.risk.irmp.workflow.client.flowable.api;

import com.ibm.risk.irmp.workflow.client.flowable.ApiClient;

import com.ibm.risk.irmp.workflow.client.flowable.model.CaseInstanceCreateRequest;
import com.ibm.risk.irmp.workflow.client.flowable.model.CaseInstanceQueryRequest;
import com.ibm.risk.irmp.workflow.client.flowable.model.CaseInstanceResponse;
import com.ibm.risk.irmp.workflow.client.flowable.model.DataResponseCaseInstanceResponse;
import com.ibm.risk.irmp.workflow.client.flowable.model.StageResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;


@Component("com.ibm.risk.irmp.flowable.api.CaseInstancesApi")
public class CaseInstancesApi {
    private ApiClient apiClient;

    public CaseInstancesApi() {
        this(new ApiClient());
    }

    @Autowired
    public CaseInstancesApi(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    public ApiClient getApiClient() {
        return apiClient;
    }

    public void setApiClient(ApiClient apiClient) {
        this.apiClient = apiClient;
    }

    /**
     * Start a case instance
     * Note that also a *transientVariables* property is accepted as part of this json, that follows the same structure as the *variables* property.  Only one of *caseDefinitionId* or *caseDefinitionKey* an be used in the request body.   Parameters *businessKey*, *variables* and *tenantId* are optional.   If tenantId is omitted, the default tenant will be used. More information about the variable format can be found in the REST variables section.   Note that the variable-scope that is supplied is ignored, process-variables are always local.
     * <p><b>200</b> - successful operation
     * <p><b>201</b> - Indicates the case instance was created.
     * <p><b>400</b> - Indicates either the case definition was not found (based on id or key), no process is started by sending the given message or an invalid variable has been passed. Status description contains additional information about the error.
     * @param body The body parameter
     * @return CaseInstanceResponse
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public CaseInstanceResponse createCaseInstance(CaseInstanceCreateRequest body) throws RestClientException {
        Object postBody = body;

        String path = UriComponentsBuilder.fromPath("/cmmn-runtime/case-instances").build().toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<CaseInstanceResponse> returnType = new ParameterizedTypeReference<CaseInstanceResponse>() {};
        return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * Delete a case instance
     *
     * <p><b>204</b> - Indicates the case instance was found and deleted. Response body is left empty intentionally.
     * <p><b>404</b> - Indicates the requested case instance was not found.
     * @param caseInstanceId The caseInstanceId parameter
     * @param deleteReason The deleteReason parameter
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public void deleteCaseInstance(String caseInstanceId, String deleteReason) throws RestClientException {
        Object postBody = null;

        // verify the required parameter 'caseInstanceId' is set
        if (caseInstanceId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'caseInstanceId' when calling deleteCaseInstance");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("caseInstanceId", caseInstanceId);
        String path = UriComponentsBuilder.fromPath("/cmmn-runtime/case-instances/{caseInstanceId}").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "deleteReason", deleteReason));

        final String[] accepts = { };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {};
        apiClient.invokeAPI(path, HttpMethod.DELETE, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * Get a case instance
     *
     * <p><b>200</b> - Indicates the case instance was found and returned.
     * <p><b>404</b> - Indicates the requested case instance was not found.
     * @param caseInstanceId The caseInstanceId parameter
     * @return CaseInstanceResponse
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public CaseInstanceResponse getCaseInstance(String caseInstanceId) throws RestClientException {
        Object postBody = null;

        // verify the required parameter 'caseInstanceId' is set
        if (caseInstanceId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'caseInstanceId' when calling getCaseInstance");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("caseInstanceId", caseInstanceId);
        String path = UriComponentsBuilder.fromPath("/cmmn-runtime/case-instances/{caseInstanceId}").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<CaseInstanceResponse> returnType = new ParameterizedTypeReference<CaseInstanceResponse>() {};
        return apiClient.invokeAPI(path, HttpMethod.GET, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * Get diagram for a case instance
     *
     * <p><b>200</b> - Indicates the case instance was found and the diagram was returned.
     * <p><b>400</b> - Indicates the requested case instance was not found but the process does not contain any graphical information (CMMN DI) and no diagram can be created.
     * <p><b>404</b> - Indicates the requested case instance was not found.
     * @param caseInstanceId The caseInstanceId parameter
     * @return List&lt;byte[]&gt;
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public List<byte[]> getCaseInstanceDiagram(String caseInstanceId) throws RestClientException {
        Object postBody = null;

        // verify the required parameter 'caseInstanceId' is set
        if (caseInstanceId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'caseInstanceId' when calling getCaseInstanceDiagram");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("caseInstanceId", caseInstanceId);
        String path = UriComponentsBuilder.fromPath("/cmmn-runtime/case-instances/{caseInstanceId}/diagram").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        final String[] accepts = { };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<List<byte[]>> returnType = new ParameterizedTypeReference<List<byte[]>>() {};
        return apiClient.invokeAPI(path, HttpMethod.GET, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     *
     *
     * <p><b>200</b> - successful operation
     * @param caseInstanceId The caseInstanceId parameter
     * @return List&lt;StageResponse&gt;
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public List<StageResponse> getStageOverview(String caseInstanceId) throws RestClientException {
        Object postBody = null;

        // verify the required parameter 'caseInstanceId' is set
        if (caseInstanceId == null) {
            throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'caseInstanceId' when calling getStageOverview");
        }

        // create path and map variables
        final Map<String, Object> uriVariables = new HashMap<String, Object>();
        uriVariables.put("caseInstanceId", caseInstanceId);
        String path = UriComponentsBuilder.fromPath("/cmmn-runtime/case-instances/{caseInstanceId}/stage-overview").buildAndExpand(uriVariables).toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<List<StageResponse>> returnType = new ParameterizedTypeReference<List<StageResponse>>() {};
        return apiClient.invokeAPI(path, HttpMethod.GET, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * List case instances
     *
     * <p><b>200</b> - Indicates request was successful and the case instances are returned
     * <p><b>400</b> - Indicates a parameter was passed in the wrong format . The status message contains additional information.
     * @param id Only return models with the given version.
     * @param caseDefinitionKey Only return case instances with the given case definition key.
     * @param caseDefinitionId Only return case instances with the given case definition id.
     * @param businessKey Only return case instances with the given businessKey.
     * @param superCaseInstanceId Only return case instances which have the given super case instance id (for cases that have a case tasks).
     * @param includeCaseVariables Indication to include case variables in the result.
     * @param tenantId Only return case instances with the given tenantId.
     * @param tenantIdLike Only return case instances with a tenantId like the given value.
     * @param withoutTenantId If true, only returns case instances without a tenantId set. If false, the withoutTenantId parameter is ignored.
     * @param sort Property to sort on, to be used together with the order.
     * @return DataResponseCaseInstanceResponse
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public DataResponseCaseInstanceResponse listCaseInstances(String id, String caseDefinitionKey, String caseDefinitionId, String businessKey, String superCaseInstanceId, Boolean includeCaseVariables, String tenantId, String tenantIdLike, Boolean withoutTenantId, String sort) throws RestClientException {
        Object postBody = null;

        String path = UriComponentsBuilder.fromPath("/cmmn-runtime/case-instances").build().toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "id", id));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "caseDefinitionKey", caseDefinitionKey));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "caseDefinitionId", caseDefinitionId));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "businessKey", businessKey));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "superCaseInstanceId", superCaseInstanceId));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "includeCaseVariables", includeCaseVariables));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "tenantId", tenantId));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "tenantIdLike", tenantIdLike));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "withoutTenantId", withoutTenantId));
        queryParams.putAll(apiClient.parameterToMultiValueMap(null, "sort", sort));

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<DataResponseCaseInstanceResponse> returnType = new ParameterizedTypeReference<DataResponseCaseInstanceResponse>() {};
        return apiClient.invokeAPI(path, HttpMethod.GET, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
    /**
     * Query case instances
     * The request body can contain all possible filters that can be used in the List case instances URL query. On top of these, it’s possible to provide an array of variables to include in the query, with their format described here.  The general paging and sorting query-parameters can be used for this URL.
     * <p><b>200</b> - Indicates request was successful and the case instances are returned
     * <p><b>400</b> - Indicates a parameter was passed in the wrong format . The status-message contains additional information.
     * @param body The body parameter
     * @return DataResponseCaseInstanceResponse
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public DataResponseCaseInstanceResponse queryCaseInstances(CaseInstanceQueryRequest body) throws RestClientException {
        Object postBody = body;

        String path = UriComponentsBuilder.fromPath("/cmmn-query/case-instances").build().toUriString();

        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        final HttpHeaders headerParams = new HttpHeaders();
        final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

        final String[] accepts = {
            "application/json"
        };
        final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
        final String[] contentTypes = { };
        final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

        String[] authNames = new String[] { "basicAuth" };

        ParameterizedTypeReference<DataResponseCaseInstanceResponse> returnType = new ParameterizedTypeReference<DataResponseCaseInstanceResponse>() {};
        return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
    }
}
